package com.borya.action.web;

import java.util.List;

import org.apache.log4j.Logger;

import com.borya.action.BaseAction;
import com.borya.framework.annotation.Action;
import com.borya.framework.annotation.Autowired;
import com.borya.framework.annotation.JsonResult;
import com.borya.framework.annotation.UrlMapping;
import com.borya.framework.core.ServerRequest;
import com.borya.framework.core.ServerResponse;
import com.borya.service.web.UploadSourceService;
import com.borya.statuscode.StatusCode;
import com.borya.util.StringUtil;

import io.netty.handler.codec.http.HttpHeaders;
import io.netty.handler.codec.http.multipart.FileUpload;


@UrlMapping("/w/upload/")
@Action
public class UploadSourceAction  extends BaseAction{

	private Logger log = Logger.getLogger(getClass());
	private boolean isClose = true;
	@Autowired("uploadSourceServiceImpl")
	private UploadSourceService uploadSource;
	
	public UploadSourceService getUploadSource() {
		return uploadSource;
	}

	public void setUploadSource(UploadSourceService uploadSource) {
		this.uploadSource = uploadSource;
	}

	@UrlMapping("phoneSource")
	@JsonResult
	public String phoneSource(ServerRequest request, ServerResponse response) {
		if(isClose){
			return StatusCode.SYSTEM_CONFIG_ERROR.json().msg("接口已注销，请更换为表同步").toString();
		}
		log.info("RECV : phoneSource");
		List<FileUpload> fileUploads = request.fileUploads();
		if(fileUploads.isEmpty() || 1 != fileUploads.size()){
			log.warn("上传内容错误");
			return StatusCode.NO_CONTENT.toJSON("必须上传单个文件");
		}
		
		response.headers().set("Access-Control-Allow-Origin", "*");
		response.headers().set("Access-Control-Allow-Methods", "POST, PUT, OPTIONS");
		final String result = getUploadSource().phoneSource(fileUploads.get(0));
		log.info("OUT :"+result);
		return result; 
	}
	
	@UrlMapping("phoneSourceUnicom")
	@JsonResult
	public String phoneSourceUnicom(ServerRequest request, ServerResponse response) {
		if(isClose){
			return StatusCode.SYSTEM_CONFIG_ERROR.json().msg("接口已注销，请更换为表同步").toString();
		}
		log.info("RECV : phoneSourceUnicom");
		List<FileUpload> fileUploads = request.fileUploads();
		if(fileUploads.isEmpty() || 1 != fileUploads.size()){
			log.warn("上传内容错误");
			return StatusCode.NO_CONTENT.toJSON("必须上传单个文件");
		}
		
		response.headers().set("Access-Control-Allow-Origin", "*");
		response.headers().set("Access-Control-Allow-Methods", "POST, PUT, OPTIONS");
		final String result = getUploadSource().unicomPhoneSource(fileUploads.get(0));
		log.info("OUT :"+result);
		return result; 
	}
	
	@UrlMapping("cardSource")
	@JsonResult
	public String cardSource(ServerRequest request, ServerResponse response) {
		if(isClose){
			return StatusCode.SYSTEM_CONFIG_ERROR.json().msg("接口已注销，请更换为表同步").toString();
		}
		log.info("RECV : cardSource");
		List<FileUpload> fileUploads = request.fileUploads();
		if(fileUploads.isEmpty() || 1 != fileUploads.size()){
			log.warn("上传内容错误");
			return StatusCode.NO_CONTENT.toJSON("必须上传单个文件");
		}
		
		response.headers().set("Access-Control-Allow-Origin", "*");
		response.headers().set("Access-Control-Allow-Methods", "POST, PUT, OPTIONS");
		final String result = getUploadSource().cardSource(fileUploads.get(0));
		log.info("OUT :"+result);
		return result; 
	}
	
	@UrlMapping("merchantFileUpload") 
	@JsonResult
	public String merchantFileUpload(ServerRequest request,ServerResponse response) {
		HttpHeaders headers = request.headers();
		String imgPath = headers.get("imgPath");
		String imgNo = headers.get("imgNo");
//		if(StringUtil.isNULL(imgPath)){
//			log.info("param imgPath error");
//			return StatusCode.PARAM_ERROR.toJSON("参数 照片路径 错误");
//		}
		if(StringUtil.isNULL(imgNo)){
			log.warn("param imgNo error");
			return StatusCode.PARAM_ERROR.toJSON("参数 照片类型 错误");
		}
		List<FileUpload> fileUploads = request.fileUploads();
		if(fileUploads.isEmpty() || 1 != fileUploads.size()){
			log.info("上传内容错误");
			return StatusCode.NO_CONTENT.toJSON("必须上传单个文件");
		}
		response.headers().set("Access-Control-Allow-Origin", "*");
		response.headers().set("Access-Control-Allow-Methods", "POST, PUT, OPTIONS");
		final String result = getUploadSource().merchantFileUpload(imgPath,imgNo,fileUploads.get(0));
		log.info("OUT :"+result);
		return result; 
	}
}
